package top.boycharse.ftp.client;

import top.boycharse.ftp.client.common.DataDTPClient;
import top.boycharse.ftp.client.common.InterpreterResponeUtil;
import top.boycharse.ftp.server.common.ThreadPoolManager;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Scanner;

public class Client {

    private String IP;

    private int port = 21;

    private Socket socket;

    private ServerSocket serverSocket;

    private static Client client;

    private Client() {

    }

    public static void start() {
        if (client == null) {
            client = new Client();
        }
        client.run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);
        // 连接服务器
        while (true) {
            try {
                System.out.println("请输入服务器的ip地址：");
                IP = scanner.nextLine();

                socket = new Socket(IP,port);
                serverSocket = new ServerSocket(socket.getLocalPort() + 1);
                break;
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("IP地址错误，请重新输入！");
            }
        }
        // 读取服务端返回数据
        BufferedReader reader = null;
        DataOutputStream output = null;
        try {
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String res = reader.readLine();
            output = new DataOutputStream(socket.getOutputStream());
            int code = InterpreterResponeUtil.getCode(res);
            if (code == 220) {
                System.out.println(InterpreterResponeUtil.getMessage(res));
            }else {
                System.out.println("连接服务器失败！");
                return;
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("未知错误！");
            try {
                socket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return;
        }
        // 获取用户输入
        String str = null;
        // 获取用户指令
        while (true) {
            System.out.println("请输入指令：");
            str = scanner.nextLine() + "\n";
            try {
                output.writeBytes(str);
                output.flush();
                if ("quit\n".equals(str.toLowerCase())) {
                    System.out.println("程序退出");
                    break;
                }
                String res = reader.readLine();
                // 处理res
                int code = InterpreterResponeUtil.getCode(res);
                DataDTPClient downLoadDTPClient = new DataDTPClient(serverSocket);
                if (code == 150) {
                    String info = res.substring(res.indexOf(' ') + 1);
                    File file = new File(info);
                    if (!file.isFile()) {
                        String msg = res.substring(res.indexOf(' ') + 1, res.lastIndexOf(' '));
                        System.out.println(msg);
                        String filename = res.substring(res.lastIndexOf(' ') + 1);
                        downLoadDTPClient.download(filename);
                        res = reader.readLine();
                        System.out.println(InterpreterResponeUtil.getMessage(res));
                        res = reader.readLine();
                        System.out.println(InterpreterResponeUtil.getMessage(res));
                    }else {
                        System.out.println("开始上传");
                        downLoadDTPClient.upload(file);
                    }

                }else if (code == 212) {
                    String msg = InterpreterResponeUtil.getMessage(res);
                    System.out.println(msg);
                    List<String> fileNames = downLoadDTPClient.getFileList();
                    for (String temp:fileNames) {
                        System.out.println(temp);
                    }
                }else {
                    System.out.println(InterpreterResponeUtil.getMessage(res));
                }
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("操作失败，请尝试重新输入指令！\n");
            }finally {

            }
        }
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
